package com.caregiver.watch.business.care.service.impl;

import com.caregiver.watch.business.user.dao.UserCareRecipientDao;
import com.caregiver.watch.business.care.model.dto.UpdateCareRecipientParam;
import com.caregiver.watch.business.user.model.bo.CareRecipientNameBo;
import com.caregiver.watch.business.user.model.po.UserCareRecipient;
import com.caregiver.watch.business.care.model.vo.CareRecipientVo;
import com.caregiver.watch.business.care.service.CareRecipientService;
import com.caregiver.watch.business.user.utils.UserInfoUtils;
import com.caregiver.watch.common.exception.business.BusinessException;
import com.caregiver.watch.common.utils.page.PageUtils;
import com.github.pagehelper.Page;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import static com.caregiver.watch.common.constant.NumberConstants.ZERO;
import static com.caregiver.watch.common.utils.page.PageUtils.ORDER_BY_UPDATE_TIME_DESC;
/**
 * 被照护人信息 服务层实现
 */
@Service
@AllArgsConstructor
public class CareRecipientServiceImpl implements CareRecipientService {

    private final UserCareRecipientDao userCareRecipientDao;
    
    /**
     * 查询被照护人信息列表
     * 
     * @param pageNum 页码
     * @param pageSize 页大小
     *
     * @return 被照护人信息集合
     */
    @Override
    public Page<UserCareRecipient> selectUserCareRecipientList(Integer pageNum, Integer pageSize, Long userId) {
        // 开启分页
        return PageUtils.selectPage(pageNum, pageSize, () -> userCareRecipientDao.selectUserCareRecipientList(userId));
    }

    @Override
    public List<UserCareRecipient> queryUserCareRecipientByIds(Long userId, List<Long> recipientIds) {
        if (CollectionUtils.isEmpty(recipientIds)) {
            return new ArrayList<>();
        }
        return userCareRecipientDao.queryUserCareRecipientByIds(userId, recipientIds);
    }

    /**
     * 通过被照护人ID查询被照护人信息
     * 
     * @param recipientId 被照护人ID
     * @return 被照护人信息
     */
    @Override
    public UserCareRecipient selectUserCareRecipientById(Long userId, Long recipientId) {
        return userCareRecipientDao.selectUserCareRecipientById(userId, recipientId);
    }
    
    /**
     * 新增被照护人信息
     * 
     * @param userCareRecipient 被照护人信息
     * @return 结果
     */
    @Override
    public Long insertUserCareRecipient(UserCareRecipient userCareRecipient) {
        int count = userCareRecipientDao.insertUserCareRecipient(userCareRecipient);
        return count > ZERO ? userCareRecipient.getRecipientId() : null;
    }
    
    /**
     * 删除被照护人信息
     * 
     * @param recipientId 被照护人ID
     * @return 结果
     */
    @Override
    public int deleteUserCareRecipientById(Long recipientId) {
        return userCareRecipientDao.deleteUserCareRecipientById(recipientId);
    }
    
    /**
     * 批量删除被照护人信息
     * 
     * @param recipientIds 需要删除的被照护人ID数组
     * @return 结果
     */
    @Override
    public int deleteUserCareRecipientByIds(List<Long> recipientIds) {
        return userCareRecipientDao.deleteUserCareRecipientByIds(recipientIds);
    }

    @Override
    public Page<CareRecipientVo> queryCareRecipientList(Integer pageNum, Integer pageSize) {
        Long userId = UserInfoUtils.getUserId();
        return PageUtils.selectPage(pageNum, pageSize,
                () -> userCareRecipientDao.selectUserCareRecipientList(userId),
                CareRecipientVo::build,
                ORDER_BY_UPDATE_TIME_DESC);
    }

    @Override
    public Boolean updateCareRecipient(UpdateCareRecipientParam updateCareRecipientParam) {
        Long recipientId = updateCareRecipientParam.getRecipientId();
        if (Objects.isNull(recipientId)) {
            throw new BusinessException("被照护人ID不能为空");
        }
        Long userId = UserInfoUtils.getUserId();
        UserCareRecipient userCareRecipient = updateCareRecipientParam.toPo(userId);
        return userCareRecipientDao.updateUserCareRecipient(userCareRecipient) > ZERO;
    }

    @Override
    public Boolean deleteCareRecipient(Long recipientId) {
        if (Objects.isNull(recipientId)) {
            throw new BusinessException("被照护人ID不能为空");
        }
        return userCareRecipientDao.deleteUserCareRecipientById(recipientId) > ZERO;
    }

    @Override
    public CareRecipientVo queryCareRecipientById(Long recipientId) {
        if (Objects.isNull(recipientId)) {
            throw new BusinessException("被照护人ID不能为空");
        }
        Long userId = UserInfoUtils.getUserId();
        UserCareRecipient userCareRecipient = userCareRecipientDao.selectUserCareRecipientById(recipientId, userId);
        if (Objects.isNull(userCareRecipient)) {
            throw new BusinessException("被照护人不存在");
        }
        return CareRecipientVo.build(userCareRecipient);
    }

    @Override
    public Boolean checkUserExist(Long userId, Long recipientId) {
        if (Objects.isNull(recipientId)) {
            return Boolean.FALSE;
        }
        return userCareRecipientDao.checkUserExist(userId, recipientId);
    }

    @Override
    public List<CareRecipientNameBo> queryCareRecipientName(Long userId) {
        if (Objects.isNull(userId)) {
            return new ArrayList<>();
        }
        return userCareRecipientDao.queryCareRecipientName(userId);
    }
}
